/*
 * 7JUL2010_q3.c
 *
 *  Created on: Jun 19, 2012
 *      Author: ubuntu
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "exame_2009_10_junho_types.h"

//preorder:proprio, esq, dir
//recursiva
void preorderRec(ArvBin a) {
	if (a) {
		printf("%d ", a->valor);
		preorderRec(a->esq);
		preorderRec(a->dir);
	}
}

void preorderItG(ArvBin a) {

	int stackMemorySize = 3;
	int stackSize = 0;
	ArvBin * stack = (ArvBin*) malloc(sizeof(ArvBin) * stackMemorySize);

	ArvBin nodo = a;

	while (nodo != NULL) {

		printf("%d ", nodo->valor);

		if (nodo->esq != NULL) {

			// push
			if (nodo->dir != NULL) {
				if (stackMemorySize == stackSize) {
					stack = (ArvBin *) realloc(stack,
							sizeof(ArvBin) * (stackMemorySize + 3));
					stackMemorySize += 3;
				}

				stack[stackSize] = nodo->dir;
				stackSize++;
			}

			nodo = nodo->esq;

			continue;
		}

		if (nodo->dir != NULL) {
			nodo = nodo->dir;
			continue;
		}

		//pop
		if (stackSize > 0) {
			nodo = stack[stackSize - 1];
			stackSize--;
			continue;

		}
		nodo = NULL;
	}
}
